package com.orhon.smartcampus.modules.student.fetcher;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.entity.Grades;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.gqlModel.GQLGrades;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSchool;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.base.mm.GradeMapper;
import com.orhon.smartcampus.modules.base.mm.SchoolMapper;
import com.orhon.smartcampus.modules.student.dao.EclassDao;
import com.orhon.smartcampus.modules.student.dao.EclassHeadteacherDao;
import com.orhon.smartcampus.modules.student.entity.Arrives;
import com.orhon.smartcampus.modules.student.entity.Eclass;
import com.orhon.smartcampus.modules.student.entity.EclassHeadteacher;
import com.orhon.smartcampus.modules.student.entity.EclassRecords;
import com.orhon.smartcampus.modules.student.gqlModel.GQLArrives;
import com.orhon.smartcampus.modules.student.gqlModel.GQLEclass;
import com.orhon.smartcampus.modules.student.gqlModel.GQLEclassRecords;
import com.orhon.smartcampus.modules.student.gqlModel.GQLHeadTeacher;
import com.orhon.smartcampus.modules.student.mm.ArrivesMapper;
import com.orhon.smartcampus.modules.student.mm.EclassHeadteacherMapper;
import com.orhon.smartcampus.modules.student.mm.EclassMapper;
import com.orhon.smartcampus.modules.student.mm.EclassRecordssMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.utils.ObjectToMap;
import com.orhon.smartcampus.utils.R;
import com.orhon.smartcampus.utils.Util;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;


@Component
public class EclasssFetcher implements DataFetcher {
	@Autowired
	EclassDao eclassDao; 

	@Autowired
	EclassHeadteacherDao eclassHeadteacherDao;

	@Autowired
	TokenService tokenService;

	@Override
	public Object get(DataFetchingEnvironment env) throws Exception {
		String token = tokenService.getSystemToken();
		if(token==null) {
			return  R.NoLogin();
		}
		Long schoolId = JwtUtils.getSchoolId(token);

		List<GQLEclass> GQLEclass = new ArrayList<>();


		GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(env);
		//String hql = " from Eclass ss left outer join fetch ss.students s left outer join fetch s.student t where 1=1 ";
		//String hql = "from Eclass as ss inner join fetch ss.students stu inner join fetch stu.student s where 1=1  ";
		//String hql = "from Eclass ss,EclassRecords er,SInformation s where 1=1  ";
	    String hql = "from Eclass ss where 1=1 ";

		if(env.containsArgument("condition")) {
			HashMap<String,Object> map = ObjectToMap.to(env.getArgument("condition"));

			if(map.get("ids") != null){
				JSONArray old_data = (JSONArray) map.get("ids");
				StringBuilder sb = new StringBuilder();
				sb.append("(");
				old_data.forEach(item -> {
					sb.append(item);
					sb.append(",");
				});
				sb.delete(sb.length()-1 , sb.length());
				sb.append(")");
				hql += " and ss.id in "+ sb.toString() + " ";
			}

			if(map.get("school_id")!=null) {
				hql += " and ss.baseSchoolsBySchoolId.id" + Util.decide(map.get("school_id"));
			}
			if(map.get("id")!=null) {
				hql += " and ss.id" + Util.decide(map.get("id"));
			}
			if(map.get("grade_id")!=null) {
				hql += " and grade_id " + Util.decide(map.get("grade_id"));
			}
			if(map.get("arrive_id")!=null) {
				hql += " and ss.arrives_id" + Util.decide(map.get("arrive_id"));
			}

			if(map.get("code")!=null) {
				hql += " and ss.eclass_code " + Util.decide(map.get("code"));
			}
			if(map.get("status")!=null) {
				hql += " and class_status" + Util.decide(map.get("status"));
			}
			if(map.get("kind_dict")!=null) {
				hql += " and ss.class_kind_dict " + Util.decide(map.get("kind_dict"));
			}
			if(map.get("nature")!=null) {
				hql += " and ss.class_nature" + Util.decide(map.get("nature"));
			}
			if(map.get("building_id")!=null) {
				hql += " and ss.buildings_id " + Util.decide(map.get("building_id"));
			}
			if(map.get("unit_id")!=null) {
				hql += " and ss.unit_id " + Util.decide(map.get("unit_id"));
			}
			if(map.get("floor_id")!=null) {
				hql += " and ss.floor_id " + Util.decide(map.get("floor_id"));
			}
			if(map.get("classroom_id")!=null) {
				hql += " and ss.classroom " + Util.decide(map.get("classroom_id"));
			}
			if(map.get("graduated_flag")!=null) {
				hql += " and ss.graduated_flag " + Util.decide(map.get("graduated_flag"));
			}
			if(map.get("eclass_number")!=null) {
				hql += " and ss.eclass_number " + Util.decide(map.get("eclass_number"));
			}
			if(map.get("gender")!=null){
				hql += " and t.sex "+Util.decide(map.get("gender"));
			}
			//hql += " and er in elements(ss.students)";
			//hql += " and s in elements(er.student)";
		}
		System.out.println(hql);
		List<Eclass> list = eclassDao.find(hql,null,page.getPage(),page.getLimit());

		for (Eclass item : list) {
			GQLGrades gqlGrades = null;
			if (env.getSelectionSet().contains("data/grade")){
				Grades grades = item.getBaseGradesByGradeId();
				gqlGrades = GradeMapper.MAPPER.toGrade(grades);
			}

			GQLArrives gqlArrives = null;
			if (env.getSelectionSet().contains("data/arrives")){
				Arrives arrives = item.getStudentArrivesByArrivesId();
				gqlArrives = ArrivesMapper.MAPPER.toArrives(arrives);
			}

			GQLSchool gqlSchool = null;
			if (env.getSelectionSet().contains("data/school")){
				Schools schools = item.getBaseSchoolsBySchoolId();
				gqlSchool = SchoolMapper.MAPPER.toSchool(schools);
			}
			List<GQLEclassRecords> stu = null;
			if (env.getSelectionSet().contains("data/students")){
				List<EclassRecords> erList = item.getStudents();
				if (erList != null && erList.size() > 0){
					stu = EclassRecordssMapper.MAPPER.toEclassRecordsList(erList);
				}
			}


			List<GQLHeadTeacher> gqlHeadTeacher = null;
			if (env.getSelectionSet().contains("data/head_teacher")){
				List<EclassHeadteacher> eclassHeadteacherList = eclassHeadteacherDao.find("from EclassHeadteacher where deletedAt = null and eclass_id = "+item.getId(),null);
				gqlHeadTeacher = EclassHeadteacherMapper.MAPPER.mapEntityList(eclassHeadteacherList);
			}

//			GQLBuilding gqlBuilding = null;
//			if (env.getSelectionSet().contains("data/building")){
//				Buildings buildings = item.getMaterialBuildingsByBuildingsId();
//				gqlBuilding = BuildingMapper.MAPPER.toBuilding(buildings);
//			}
//
//			GQLUnit gqlUnit = null;
//			if (env.getSelectionSet().contains("data/unit")){
//				Unit unit = item.getMaterialUnitByUnitId();
//				gqlUnit = UnitMapper.MAPPER.toUnit(unit);
//			}
//
//			GQLFloor gqlFloor = null;
//			if (env.getSelectionSet().contains("data/floor")){
//				Floor floor = item.getMaterialFloorByFloorId();
//				gqlFloor = FloorMapper.MAPPER.toFloor(floor);
//			}
//
//			GQLRoom gqlRoom = null;
//			if (env.getSelectionSet().contains("data/classroom")){
//				Room room = item.getRoom();
//				gqlRoom = RoomMapper.MAPPER.toRoom(room);
//			}

			//            GQLSite gqlSite = null;
			//            if (env.getSelectionSet().contains("site")){
			//               Floor floor = item.get
			//               gqlFloor = FloorMapper.MAPPER.toFloor(floor);
			//            }
			GQLEclass eclass = EclassMapper.MAPPER.toEclass(item);
			eclass.setGrade(gqlGrades);
			eclass.setArrives(gqlArrives);
			eclass.setSchool(gqlSchool);
			eclass.setStudents(stu);
			eclass.setHead_teacher(gqlHeadTeacher);
//			eclass.setBuilding(gqlBuilding);
//			eclass.setUnit(gqlUnit);
//			eclass.setFloor(gqlFloor);
//			eclass.setClassroom(gqlRoom);
			GQLEclass.add(eclass);
		}

		int total = 0;
		if (env.getSelectionSet().contains("total")){
			total = eclassDao.total(hql,null);
		}
		return Result.ok().put("data", GQLEclass).put("total", total);
	}


}
